// pages/photos/photos.js
var postsData = require('../../data/data.js');
var common = require("../../utils/common.js");
var app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    listData:[],
    roomTotal:[],
    roomTotal2: [],
    roomTotalA:[],
    activeIndex:0,
    currentTab:0,
    itemChange:[],
    allRoomNum:0,
    householdNum: 0,
    sampleRoomNum: 0,
    realRoomNum: 0,
    otherRoomNum: 0,
    itemImg: "",
    itemActive:'',
    hasWaterLogo:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar();
    getApp().editTabBar();
    if (app.globalData.xcxtype.hasLogo == '1') {
      //显示水印
      this.setData({
        hasWaterLogo: true
      })
    } else {
      this.setData({
        hasWaterLogo: false
      })
    };
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log("onReady");
    // var activeId = this.data.itemActive;
    // console.log(activeId)
    // this.allRoom(1, 0, 0, false);
  },

  onShow: function () {
    wx.setNavigationBarTitle({
      title: '楼盘相册'
    });
   
    // var activeId = this.data.itemActive;
    //  console.log(activeId)
    var _that = this;
    common.ajaxPost(
      'portal/wlpPhotoType/queryPhotoType.action', {
        bid: app.globalData.bid
      },
      function (res) {
        if (res && res.length > 0) {
          console.log(res);
          var itemActiveId = res[0].id;
          console.log(itemActiveId)
          //获取当前的活动id
          var data = res;
          _that.setData({
            listData: data,
            itemActive: itemActiveId  
          })
          _that.allRoom(res[_that.data.currentTab].id, 0, _that.data.currentTab, false);
          //_that.update()
        } else {
          wx.showToast({
            title: '暂无相关数据',
            icon: 'none'
          })
        }
      }
    )
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      wx.stopPullDownRefresh();
  },
  review:function(e){
    console.log(e);
    console.log(this.data.itemChange)
    var imgList=[]
    for (var i = 0; i <this.data.itemChange.length; i++){
      console.log(this.data.itemChange[i])
      imgList.push(this.data.itemChange[i].url)
    }
    console.log(imgList)
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     var that=this;
     var nowIndex = that.data.activeIndex;
     var _length=15;
     var stNum = that.data.allRoomNum;
     var itemActive = that.data.itemActive;
     if (stNum >= that.data.itemTotal) {
       wx.showToast({
         title: '没有更多了',
         icon: 'none'
       })
     } else {
       stNum = Math.floor(stNum + _length);
       console.log(stNum);
       that.allRoom(itemActive, stNum, 0, true);
       that.setData({
         allRoomNum: stNum
       })
     }


    //  if(nowIndex===0){
    //    /**类型A */
    //    var stNum = that.data.allRoomNum; 
    //    if (stNum>=that.data.itemTotal){
    //      wx.showToast({
    //        title: '没有更多了',
    //        icon: 'none'
    //      })
    //    }else{
    //      stNum = Math.floor(stNum + _length);
    //      console.log(stNum);
    //      that.allRoom(1,stNum,0,true);
    //      that.setData({
    //        allRoomNum: stNum
    //      })
    //    }
    //  }
    //  if (nowIndex === 1) {
    //    /**类型A */
    //    var stNum = that.data.householdNum;
    //    if (stNum >= that.data.itemTotal) {
    //      wx.showToast({
    //        title: '没有更多了',
    //        icon: 'none'
    //      })
    //    } else {
    //      stNum = Math.floor(stNum + _length);
    //      console.log(stNum);
    //      that.allRoom(5, stNum,1,true);
    //      that.setData({
    //        householdNum: stNum
    //      })
    //    }
    //  }
  },

  /**获取数据 */
  allRoom:function(id,i,index,typeAll){
    var _that=this;
    console.log(index)
    var _thatId=id;
    common.ajaxPost('portal/wlpPhotos/queryPhotos.action',
      {
        start: i,
        length: 15,
        typeId: _thatId,
        bid: app.globalData.bid
      },
      res => {
        var roomTotal = _that.data.roomTotal;
        var roomTotalA = _that.data.roomTotalA;
        var roomTotal2 = _that.data.roomTotal2;
        var roomData=res.data;
        var itemActive = _that.data.itemActive;
        if(typeAll==false){
          if (id == itemActive){
            //点击进来的
            roomTotal = [];
            _that.data.allRoomNum="0";
            for (var i = 0; i < roomData.length; i++) {
              roomData[i]["hasWaterLogo"]=(_that.data.hasWaterLogo);
              roomTotal.push(roomData[i]);
            }
           
            _that.setData({
              itemChange: roomTotal,
              itemTotal: res.recordsTotal,
              roomTotalA: roomTotal
            })
            if (res.recordsTotal==0){
             wx.showToast({
               title: '暂无数据',
               icon:'none'
             })
            }
          }
          else{
            roomTotal = [];
            _that.data.allRoomNum = "0";
            for (var i = 0; i < roomData.length; i++) {
              roomTotal.push(roomData[i]);
            }
            _that.setData({
              itemChange: roomTotal,
              itemTotal: res.recordsTotal,
              roomTotalA: roomTotal
            })
          }
        }else{
          for (var i = 0; i < roomData.length; i++) {
              roomTotalA.push(roomData[i]);
            }
            _that.setData({
              itemChange: roomTotalA
            })
        }  
      }
    )
  },
  /*点击事件*/
  tapActive: function(e){
    wx.showToast({
      icon: 'loading'
    })
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var _that = this;
    _that.setData({ currentTab: index});
   
      _that.allRoom(id, 0, index, false);
      _that.setData({ itemActive: id });
      wx.hideToast()
    

    // if(index=="0"){
    //   _that.allRoom(id,0,index,false);
    //   wx.hideLoading()
    // }else if(index=="1"){
    //   _that.allRoom(id,0,index,false);
    //   wx.hideLoading()
    // }
   
    
  },
  onShareAppMessage: function (res) {
    var that = this;
    var xcxids;
    if (app.globalData.xcxshareid) {
      xcxids = app.globalData.xcxshareid
    } else {
      xcxids = app.globalData.queryWlpXcxByParams[0].id
    }

    return {
      title: '楼盘相册',
      path: '/pages/index/index?type=photos&bid=' + app.globalData.bid + "&isIndex=1" + "&xcxid=" + xcxids + "&lpid=" + app.globalData.lpid,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  changeTab: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.switchTab({
      url: url
    })
  },
})